<div class="form">
<script language="javascript" type="text/javascript">
//alert(1);
	function report()
	{
		//alert(111);
		var class_id = document.getElementById('StudentAttendance_class_id').value;
		var from_date = document.getElementById('StudentAttendance_attendance_date').value;
		var to_date = document.getElementById('StudentAttendance_current_date').value; 
		var no_of_days = document.getElementById('no_of_days').value;
		if(from_date>to_date)
		{
			alert("invalid date selection");
		}
		else if (isNaN(no_of_days)) 
		{
			alert("Please Enter Valid Number");
		}
		else
		{
			//alert(class_id);
			if(from_date!="" && class_id!="" && to_date!="")
			{
				if(class_id!=0)
				{
					//header('url='. $this->createUrl('controller/action'));
					window.location.href= 'index.php?r=StudentAttendance/reporttwo&class_id='+class_id+'&from_date='+from_date+'&to_date='+to_date+'&no_of_days='+no_of_days;	
					//window.location.href= 'index.php?r=StudentAttendance/report&class_id='+class_id+'&from_date='+from_date;	
				}
				else
				{
					//alert(4);
					window.location= 'index.php?r=StudentAttendance/reporttwo&from_date='+from_date+'&to_date='+to_date+'&no_of_days='+no_of_days;		
				}
			}
			else
			{
				alert("Class Name and Date cannot be blank.");
			}
		}
	}
</script>
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'student-attendance-report-form',
	'enableAjaxValidation'=>false,
	'htmlOptions'=>array('onsubmit'=>"return false;"),
)); ?>
<?php
$this->breadcrumbs=array(
	'Student Attendances'=>array('index'),
	'Report',
);

$this->menu=array( array('label'=>'Student Attendance', 'url'=>array('create')),
					   array('label'=>'Report on a daily basis', 'url'=>array('reportone')),
					   array('label'=>'Report of Students who are absent for more than 2 days', 'url'=>array('reporttwo')),
					   array('label'=>'Report of Students who are absent for less than 2 days', 'url'=>array('reportthree')),
					  );

?>
	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>
	<div id="content_main">
		<div class="section_box">
			<div class="section_box_header">
				<h2> Attendance Report </h2>
			</div>
		</div>
		<div>
			<label class="required_sign">  </label>
		</div>
		
		<div class="column_equal">
			<div class="row">
				<div class="frmlabel">
					<?php echo $form->labelEx($model,'class_id'); ?>
				</div>
				<div class="frmvalue">
					<?php $get_class = $model->Get_class(); ?>
					<select name="StudentAttendance_class_id" id="StudentAttendance_class_id">
						<option value="" Selected="selected"> SELECT </option>
						<?php foreach($get_class as $class) { ?>
							<option value="<?php echo $class['class_id']; ?>" > <?php echo $class['class_name']; ?></option>
						<?php } ?>
						<option value="0" > Report for Entire school </option>
					</select>

				</div>
			</div>

			<div class="row">
				<div class="frmlabel">
					<label>Enter Number Of Day's</label>
				</div>
				<div class="frmvalue">
					<input type="number" id="no_of_days" name="no_of_days" />
				</div>
				
			</div>
			
			<div class="row">
				<div class="frmlabel">
					<?php echo $form->labelEx($model,'Date'); ?>
				</div>
				<div class="frmvalue">
					<?php  $this->widget('ext.my97DatePicker.JMy97DatePicker',array(
						'name'=>CHtml::activeName($model,'attendance_date'),
						'value'=>$model->attendance_date,
						'options'=>array('dateFmt'=>'yyyy-MM-dd'),
						)); 
					?>
					<?php echo $form->error($model,'attendance_date'); ?>
				</div>
				
			</div>
		
			<div class="row">
				<div class="frmlabel">
					<?php echo $form->labelEx($model,'To Date'); ?>
				</div>
				<div class="frmvalue">
					<?php  $this->widget('ext.my97DatePicker.JMy97DatePicker',array(
						'name'=>CHtml::activeName($model,'current_date'),
						'value'=>$model->current_date,
						'options'=>array('dateFmt'=>'yyyy-MM-dd'),
						)); 
					?>
					<?php echo $form->error($model,'current_date'); ?>
				</div>
				
			</div>  

			<div class="row buttons">
				<div class="frmlabel"></div>
				<div class="frmvalue">
					<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array('class' => 'submitClass','onclick'=>'report(this)')); ?>
				</div>
			</div>
		</div>
		<div class="clearfix"></div>
		<h2> Report </h2>
		
		
		<div class="tableinnerlist">
			<div align="center"><strong>Class-wise Attendance Report as on - <font color="red"><?php if(isset($_REQUEST['from_date'])){ echo $_REQUEST['from_date'];}?></font> to <font color="red"><?php if(isset($_REQUEST['to_date'])){ echo $_REQUEST['to_date'];}?></font><br/> Report of Students who are absent for more than 2 days</strong> </div>
			<table border="0" cellpadding="0" cellspacing="0" width="80%">
				
				<tr>
					<th>Sr No</th>
					<th>Class</th>
					<th>Name of the Student</th>
					<th>Roll No. of the Student</th>
					<th>Absent from</th>
					<th>Total Number of Days Absent in the Academic Year</th>
					<th>Contact Number of</th>
				</tr>
				
				<?php if(isset($_REQUEST['class_id']))
				{ 
					$no_ofdays=$_REQUEST['no_of_days'];
					if($no_ofdays!="")
					{
						$no_of_days=$_REQUEST['no_of_days'];
						
					else
					{
					//$no_of_days=2;
					//echo "test = ".$no_of_days;
					}
					$get_student=$model->Students_absent_morethan_twodays($_REQUEST['no_of_days'],$_REQUEST['class_id'],$_REQUEST['from_date'],$_REQUEST['to_date']);
					
					$serial=1;
					foreach($get_student as $result)
					{
				?>
						<tr>
							<td><?php echo $serial++; ?></td>
							<td>
								<?php $classtbl = Classtbl::model()->findByAttributes(array('class_id'=>$_REQUEST['class_id'])); 
									echo $classtbl->class_name;
								?>
							</td>
							<td><?php echo $result['first_name'],' ', $result['last_name'] ; ?></td>
							<td><?php echo $result['registration_no']; ?></td>
							<td><?php echo $_REQUEST['from_date'],' to ',$_REQUEST['to_date']; ?></td>
							<td><?php $no_of_days_absent = (int)$result['totalabsent']; 
								echo $no_of_days_absent;
								?>
							</td>
							<td><?php echo $result['cell_number']; ?></td>
						</tr>
				<?php }
				}
				else if(isset($_REQUEST['from_date']))
				{
					
				?>
				<?php $classname = Classtbl::model()->findAll(); 
					$serial = 1;
					foreach($classname as $class_name)
					{ 
						$class_id=$class_name->class_id;
						$get_student=$model->Students_absent_morethan_twodays($_REQUEST['no_of_days'],$class_id,$_REQUEST['from_date'],$_REQUEST['to_date']);
						//$sql = "SELECT first_name, last_name,attendance_date,cell_number, registration_no,count( student_id ) AS totalabsent FROM student_attendance sa, registration sr WHERE sa.student_id=sr.registration_no and  sa.student_id = sr.registration_no and  sa.class_id='$class_name->class_id' and sa.attendance_date BETWEEN '$_REQUEST[from_date]' and '$_REQUEST[to_date]' GROUP BY student_id HAVING count( sa.student_id )>'$no_of_days'";
						//$command = Yii::app()->db->createCommand($sql);
						//$query_results = $command->queryAll();
						foreach($get_student as $result)
						{
				?>
							<tr>
								<td><?php echo $serial++; ?></td>
								<td><?php echo $class_name->class_name; ?></td>
								<td><?php echo $result['first_name'],' ', $result['last_name'] ; ?></td>
								<td><?php echo $result['registration_no']; ?></td>
								<td><?php echo $_REQUEST['from_date'],' to ',$_REQUEST['to_date']; ?></td>
								<td><?php $no_of_days_absent = (int)$result['totalabsent']; 
									echo $no_of_days_absent;
									?>
								</td>
								<td><?php echo $result['cell_number']; ?></td>
							</tr>
				<?php
						}
					}
				}
				?>
			</table>
		</div>
		
	</div>
	<?php 
	
		if(isset($_REQUEST['class_id']))
		{
			
	?>
		<div class="ea_pdf" style="top:4px; right:6px;"><?php echo CHtml::link('<img src="images/pdf_report_button.png">', array('StudentAttendance/pdf_of_reporttwo','class_id'=>$_REQUEST['class_id'],'from_date'=>$_REQUEST['from_date'],'to_date'=>$_REQUEST['to_date'],'no_of_days'=>$_REQUEST['no_of_days']),array('target'=>'_blank')); ?></div>
	<?php 
		}
		else if(isset($_REQUEST['from_date']))
		{
		$class_id="1";
	?>
		<div class="ea_pdf" style="top:4px; right:6px;"><?php echo CHtml::link('<img src="images/pdf_report_button.png">', array('StudentAttendance/reporttwoforentireschool','classid'=>$class_id,'from_date'=>$_REQUEST['from_date'],'to_date'=>$_REQUEST['to_date'],'no_of_days'=>$_REQUEST['no_of_days']),array('target'=>'_blank')); ?></div>
	<?php } ?>
<?php $this->endWidget(); ?>

</div><!-- form -->